#include "readsavexml.h"
#include <QFile>

ReadSaveXml::ReadSaveXml(QObject *parent) :QObject(parent)
{

}


ReadSaveXml::~ReadSaveXml()
{
}

bool ReadSaveXml::SaveXml(QString sPath, QVector<NodeXml> vctXml)
{
	QDomDocument doc("");
	for (int i = 0;i<vctXml.size();i++)
	{
		QDomElement docroot = doc.createElement(vctXml.at(i).sName);
		for (QMap<QString,QString>::const_iterator iterAttr = vctXml.at(i).mapAttr.begin();iterAttr != vctXml.at(i).mapAttr.end();iterAttr ++)
		{
			docroot.setAttribute(iterAttr.key(), iterAttr.value());
		}
		if (vctXml.at(i).vctNode.size()>0)//设置子节点或者内容
		{
			foreach (NodeXml nodeIter ,vctXml.at(i).vctNode)
			{
				SaveXmlChild(&docroot, nodeIter, doc);
			}
		}else if (vctXml.at(i).sValue != "")
		{
			docroot.setNodeValue(vctXml.at(i).sValue);
		}
		doc.appendChild(docroot);
	}
	QFile preferFile(sPath);
	if (!preferFile.open(QIODevice::WriteOnly))
	{
		return false;
	}
	QString sText = "";
	sText = doc.toString();
	sText = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \r\n" + sText;
	preferFile.write(sText.toStdString().c_str());
	preferFile.close();
	return true;
}

QVector<NodeXml> ReadSaveXml::ReadXml(QString sPath)
{
	QVector<NodeXml> vctXml;
	vctXml.clear();
	if (sPath == "")
	{
		return vctXml;
	}
	QDomDocument doc;
	QFile file(sPath);
	if (file.exists(sPath) == false)
	{
		return vctXml;
	}
	if (!file.open(QIODevice::ReadOnly))
	{
		return vctXml;
	}
	if (!doc.setContent(&file))
	{
		file.close();
		return vctXml;
	}
	else
		file.close();

	QDomElement docElem = doc.documentElement();
	while (!docElem.isNull())
	{
		NodeXml nodeXml;
		ReadXmlChild(&docElem, nodeXml);
		vctXml.push_back(nodeXml);
		docElem = docElem.nextSiblingElement();
	}
	return vctXml;
}

bool ReadSaveXml::ReadXmlChild(QDomElement *pXmlElement, NodeXml &nodeXml)
{
	if (pXmlElement == NULL)
		return false;
	nodeXml.sName = pXmlElement->tagName();//获取名字
	QDomNamedNodeMap domNameNodeMap = pXmlElement->attributes();
	for (int i = 0;i<domNameNodeMap.count();i++)//获取属性
	{
		QDomNode domNode = domNameNodeMap.item(i);
		nodeXml.mapAttr[domNode.nodeName()] = domNode.nodeValue();
	}
	QDomNode domNode = pXmlElement->firstChild();

	while (!domNode.isNull())//获取子节点
	{
		QDomText doText = domNode.toText();
		if (!doText.isNull())
		{
			nodeXml.sValue = doText.nodeValue();
		}
		QDomElement e = domNode.toElement();//try to convert the node to an element.
		if (!e.isNull())
		{
			NodeXml nodeXmlChild;
			ReadXmlChild(&e, nodeXmlChild);
			nodeXml.vctNode.push_back(nodeXmlChild);
		}
		domNode = domNode.nextSibling();
	}
	return true;
}

void ReadSaveXml::SaveXmlChild(QDomElement *pDomDocument, NodeXml nodeXml, QDomDocument doc)
{
	if (pDomDocument == NULL)
		return;
	QDomElement domDocChild = doc.createElement(nodeXml.sName);
	domDocChild.setTagName(nodeXml.sName);//设置名称
	for (QMap<QString,QString>::iterator iterAttr = nodeXml.mapAttr.begin();iterAttr != nodeXml.mapAttr.end();iterAttr++)
	{
		domDocChild.setAttribute(iterAttr.key(), iterAttr.value());
	}
	if (nodeXml.vctNode.size()>0)//设置子节点或者内容
	{
		foreach (NodeXml nodeIter,nodeXml.vctNode)
		{
			SaveXmlChild(&domDocChild, nodeIter, doc);
		}
	}else if (nodeXml.sValue != "")
	{
		QDomText domTextva = doc.createTextNode(nodeXml.sValue);
		domDocChild.appendChild(domTextva);
	}
	pDomDocument->appendChild(domDocChild);
}

bool ReadSaveXml::QueryChild(const NodeXml &nodeXml, QString sName, NodeXml &resultXml)
{
	foreach(NodeXml iteXml,nodeXml.vctNode)
	{
		if (iteXml.sName == sName)
		{
			resultXml = iteXml;
			return true;
		}
	}
	foreach(NodeXml iteXml, nodeXml.vctNode)
	{
		QueryChild(iteXml, sName, resultXml);
	}
	return false;
}
